/*
	       File: Form1.cs
	Create Date: 2007-08-01
	    Version:

	Description:


	 Change log:


*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using GuerillaProgrammer.FileProcessor;
using GuerillaProgrammer.FileProcessor.Settings;
using GuerillaProgrammer.Components.GPThreadPool;
namespace ProcessorTestApp
{
    public delegate void AddMessageDelegate(string message);

    public partial class Form1 : Form, PollingCallback, IWorkItemProcessor
    {
        #region Private member fields
        private PollingController _PC;
        private AddMessageDelegate _MessageDelegate;
        private bool _KeepGoing;
        Configuration _Configuration;
        DirectoryProcessorController _DirectoryProcessorController;
        FileProcessingController _FileProcessorController;
        #endregion

        #region Constructors and cleanup methods
        public Form1()
        {
            InitializeComponent();
            _MessageDelegate = new AddMessageDelegate(this.AddMessage);
                        
            string configPath = System.Configuration.ConfigurationManager.AppSettings["ConfigFilePath"];
            _Configuration = ConfigurationManager.ReadConfiguration(configPath);

            _FileProcessorController = new FileProcessingController(_Configuration);
            _DirectoryProcessorController = new DirectoryProcessorController(_Configuration, _FileProcessorController);
        }
        #endregion

        #region Private implementation methods
        private void AddMessage(string message)
        {
            lstResults.Items.Add(message);
        }
        private void FileProcessor()
        {
            _DirectoryProcessorController.ProcessDirectories();
        }
        public void ProcessWorkItem(WorkItem workItem)
        {

        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnRun_Click(object sender, EventArgs e)
        {
            _KeepGoing = true;
            _PC = new PollingController(5, (PollingCallback)this);
            _PC.StartPollingInterval();
        }
        private void btnStop_Click(object sender, EventArgs e)
        {
            _KeepGoing = false;
        }
        #endregion

        #region Public interface
        public void Execute()
        {
            string message = string.Format("Timer - {0}", DateTime.Now.ToString("hh:mm:ss"));
            lstResults.Invoke(_MessageDelegate, new object[] { message });
            FileProcessor();
            if (true == _KeepGoing)
            {
                _PC.StartPollingInterval();
            }
            else
            {
                lstResults.Invoke(_MessageDelegate, new object[] { "last run!" });
            }
        }
        #endregion

    }
}